package sg.edu.ntu.metaweb.storage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.URISyntaxException;

import sg.edu.ntu.metaweb.storage.data.Comment;
import sg.edu.ntu.metaweb.storage.data.Link;
import sg.edu.ntu.metaweb.storage.data.User;
import sg.edu.ntu.metaweb.storage.data.Webpage;


public class TestSerialize {

	public static void main(String args[]) {
		
		System.out.println("Test - Serialize");
		
		User user = new User("Alice", "a", "alice@example.com");
		
		Webpage page = new Webpage("http://www.google.com.sg/");
		
		
		page.addLink(user, "http://www.reddit.com", Link.LINK_DIRECTION___OUTGOING);
		page.addLink(user, "http://www.reddit.com", Link.LINK_DIRECTION___OUTGOING);

		page.addTag(user, "Chile");
		page.addTag(user, "Chile");
		
		page.addComment(user, "Nice article");
		page.addComment(user, "Nice article");
		
		page.addRating(user, (byte)4);
		page.addRating(user, (byte)3);
		
		Comment comment = new Comment(user, "Nice article");
		page.addComment(comment);
		if (comment.addReply(user, "reply")) {
			System.out.println("reply successful");
		}
		
		try { 
			ObjectOutput out = new ObjectOutputStream(new FileOutputStream(page.getWebpageID() + ".dat")); 
			out.writeObject(page); 
			out.writeObject(page); 
			out.close();  
		}
		catch (IOException e) {
			// TODO: handle exception
			System.out.println(e);
		}
		
		
		try { 
			String url = "http://www.google.com.sg/";
			File file = new File(url.hashCode() + ".dat"); 
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
			
			Webpage readPage = (Webpage)in.readObject();
			in.close();
			
			try {
				System.out.println(readPage.getUrl().toURI());
				System.out.println(readPage.getUrl().toURI().toString().hashCode());
				System.out.println(readPage.getUrl().getQuery());
			}
			catch(URISyntaxException exc) {
				exc.printStackTrace();
			}
		} 
		catch (ClassNotFoundException exc) {
			System.out.println(exc);
		}
		catch (IOException exc) { 
			System.out.println(exc);
		} 
		
		
	}
	
}
